package com.hpc.Service.Impl;

import Utils.JSONUtil;
import com.hpc.Service.userService;
import com.hpc.VO.msgVo;
import entity.message;
import entity.user;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class userServiceImpl implements userService {
    @Resource
    private com.hpc.mapper.userMapper userMapper;
    @Resource
    private com.hpc.Service.openfeignService openfeignService;
    @Resource
    private RedisTemplate redisTemplate;
    public user login(String username, String password) {
        return userMapper.login(username,password);
    }

    @Override
    public List<user> getfollowers(List<String> userIds) {
        return userMapper.getFollowers(userIds);
    }

    public user getUserInfoByArticleId(String articleId) {
        user userInfoByArticleId = userMapper.getUserInfoByArticleId(articleId);
        return userInfoByArticleId;
    }

    public List<user> getUserInfoOfComment(List<String> userIds) {
        ArrayList<user> users = new ArrayList<>();
        for (String userId : userIds) {
            user userInfoOfComment = userMapper.getUserInfoOfComment(userId);
            users.add(userInfoOfComment);
        }
        return users;
    }

    @Override
    public int modifyUserInfo(user user) {
        int sex=user.getSex()=="男"?1:0;
        int i = userMapper.modifyUserInfo(user,sex);
        return i;
    }

    @Override
    public user getUserInfoBySerial(String userId) {
        return userMapper.getUserInfoBySerial(userId);
    }

    @Override
    public Map<String, String> personalHomePageRelated(String userId,String userIdIntoken) {
        user info = userMapper.getUserInfoBySerial(userId);
        HashMap<String, String> infos = new HashMap<>();
        infos.put("user", JSONUtil.toJSONString(info));
        String allArticleByUserId = openfeignService.getAllArticleByUserId(userId,true);
        infos.put("allarticle",allArticleByUserId);
        if (userId.equalsIgnoreCase(userIdIntoken)){

        }else {
            Boolean aBoolean = redisTemplate.hasKey(userIdIntoken+"_follow");
            if (aBoolean){
                boolean flag=true;
                List<String> range = redisTemplate.opsForList().range(userIdIntoken + "_follow", 0, -1);
                for (String s : range) {
                    if (userId.equalsIgnoreCase(s)){
                        infos.put("isFollowed","1");
                        flag=false;
                        break;
                    }
                }
                if (flag){
                    infos.put("isFollowed","0");
                }
            }else {
                infos.put("isFollowed","0");
            }
        }
        return infos;
    }

    @Override
    public int changeHeadSculptureUrl(String url,String userid) {
        return userMapper.changeHeadSculptureUrl(url,userid);
    }

    @Override
    public List<msgVo> msgsOfUser(String userid) {
        return userMapper.msgsOfUser(userid);
    }
    /**
     * @author: Liujd
     * @createTime: 2023/5/16 16:12
     * @description: register
     * @param:[user] 用户实体
     * @return:int
     */
    @Override
    public int register(user user) {
        user.setSerial(UUID.randomUUID().toString());
        int i = userMapper.checkIfrepeat(user.getUsername());
        if (i==0) {
            return userMapper.register(user);
        }else {
            return 0;
        }
    }

    @Override
    public int addMessage(String userId, String message) {
        entity.message message1 = new message();
        message1.setSerial(UUID.randomUUID().toString());
        message1.setMessage(message);
        message1.setMsgTime(new Date());
        message1.setUserId(userId);
        return userMapper.addMessage(message1);
    }

    @Override
    public List<message> allMessages() {
        return null;
    }
}
